package com.design.android.personnel.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import java.io.File;

public class DatabaseOpenHelper extends SQLiteOpenHelper {
    public DatabaseOpenHelper(Context context) {
        super(context, getMyDatabaseName(context), null, 1);
    }

    public static String getMyDatabaseName(Context context) {
        boolean isSdcardEnable = false;
        String state = Environment.getExternalStorageState();
        //SDCard是否插入
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            isSdcardEnable = true;
        }
        String dbPath = null;
        if (isSdcardEnable) {
            File storageDirectory = context.getExternalFilesDir("database");
            dbPath = storageDirectory.getPath();
        } else {
            //未插入SDCard，建在内存中
            dbPath = context.getFilesDir().getPath() + File.separator + "database";
        }
        File dbp = new File(dbPath);
        if (!dbp.exists()) {
            dbp.mkdirs();
        }
        return dbPath + File.separator + "database.db";
    }

    /**
     * 数据库创建时会调用,在这里运行创建表的语句
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String user = "create table if not exists user" +
                "(" +
                "id integer primary key autoincrement," +
                "phone text not null," +
                "password text not null," +
                "salt text not null," +
                "username text not null," +
                "sex text not null," +
                "createTime text not null," +
                "updateTime text not null," +
                "remark text" +
                ");";
        db.execSQL(user);

        String record = "create table if not exists personnel_info" +
                "(" +
                "id integer primary key autoincrement," +
                "userId integer not null," +
                "avatar blob," +
                "phone text not null," +
                "name text not null," +
                "sex varchar not null," +
                "addr text," +
                "createTime text not null," +
                "updateTime text not null" +
                ");";
        db.execSQL(record);


        String userLoginLog = "create table if not exists user_login_log" +
                "(" +
                "id integer primary key autoincrement," +
                "login_account text not null," +
                "login_time text not null," +
                "result text not null," +
                "result_info text" +
                ");";
        db.execSQL(userLoginLog);
    }

    /**
     * 数据库升级时，此方法会调用,在这里运行数据库更新操作
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    /**
     * 数据库打开时，此方法会调用
     */
    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
    }
}
